/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MessageSender.java
 *
 * Created on 11/03/2009, 20:35:00
 */
package br.unisc.pdaapp.ui.messages;

import br.unisc.pdaapp.constants.IPanels;
import br.unisc.pdaapp.meta.IForm;
import br.unisc.pdaapp.meta.User;
import br.unisc.pdaapp.ui.MoodleMobileFrame;
import br.unisc.pdaapp.webservice.client.WSHandler;

/**
 *
 * @author guilherme
 */
public class MessagesSenderPanel extends javax.swing.JPanel implements IForm
{
    private static MessagesSenderPanel instance;

    private String targetUsers;

    /** Creates new form MessageSender */
    private MessagesSenderPanel()
    {
        initComponents();
        targetUsers = "";
    }

    public static MessagesSenderPanel getInstance()
    {
        if (instance == null)
        {
            instance = new MessagesSenderPanel();
        }

        return instance;
    }

    public void setHeader(String text)
    {
    }

    public void setTargetUsers(String targetUsers)
    {
        this.targetUsers = targetUsers;
    }
    
    private boolean isMessageValid()
    {
        if (messageTextArea.getText().trim().length() == 0)
        {
            warningLabel.setText("Escreva uma mensagem!");
            return false;
        }

        if (targetUsers.length() == 0)
        {
            warningLabel.setText("Selecione o(s) destino(s)!");
            return false;
        }

        warningLabel.setText(" ");

        return true;
    }

    private void sendMessage()
    {
        if (!isMessageValid())
            return;

        try
        {
            WSHandler wsHandler = new WSHandler();

            User user = MoodleMobileFrame.getInstance().getUser();

            int result = wsHandler.sendMessage(user.getUserName(), targetUsers, messageTextArea.getText());

            warningLabel.setText(result != -1 ? "Mensagem enviada!" : "Erro: Mensagem não enviada!");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        backButton1 = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        warningLabel = new javax.swing.JLabel();
        centerPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        selectTargetsButton = new javax.swing.JButton();
        sendButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        messageTextArea = new javax.swing.JTextArea();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        backButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/back.jpeg"))); // NOI18N
        backButton1.setBorder(null);
        backButton1.setOpaque(false);
        backButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 1);
        jPanel1.add(backButton1, gridBagConstraints);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/close.jpeg"))); // NOI18N
        exitButton.setBorder(null);
        exitButton.setOpaque(false);
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(exitButton, gridBagConstraints);

        warningLabel.setFont(new java.awt.Font("Tahoma", 2, 10));
        warningLabel.setForeground(new java.awt.Color(255, 0, 0));
        warningLabel.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(warningLabel, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.NORTH);

        centerPanel.setBackground(new java.awt.Color(255, 255, 255));
        centerPanel.setLayout(new java.awt.BorderLayout());

        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.GridBagLayout());

        selectTargetsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/selectUser.jpeg"))); // NOI18N
        selectTargetsButton.setText("Selecionar");
        selectTargetsButton.setContentAreaFilled(false);
        selectTargetsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectTargetsButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(1, 3, 1, 2);
        jPanel2.add(selectTargetsButton, gridBagConstraints);

        sendButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/success.png"))); // NOI18N
        sendButton.setText("Enviar");
        sendButton.setContentAreaFilled(false);
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(1, 3, 1, 2);
        jPanel2.add(sendButton, gridBagConstraints);

        centerPanel.add(jPanel2, java.awt.BorderLayout.NORTH);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Mensagem"));
        jPanel3.setOpaque(false);
        jPanel3.setLayout(new java.awt.BorderLayout());

        messageTextArea.setColumns(20);
        messageTextArea.setRows(5);
        jScrollPane1.setViewportView(messageTextArea);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        centerPanel.add(jPanel3, java.awt.BorderLayout.CENTER);

        add(centerPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void backButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_backButton1ActionPerformed
    {//GEN-HEADEREND:event_backButton1ActionPerformed
        MoodleMobileFrame.getInstance().switchPanel(IPanels.MESSAGES_PANE);
}//GEN-LAST:event_backButton1ActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitButtonActionPerformed
    {//GEN-HEADEREND:event_exitButtonActionPerformed
        MoodleMobileFrame.getInstance().quit(this);
}//GEN-LAST:event_exitButtonActionPerformed

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_sendButtonActionPerformed
    {//GEN-HEADEREND:event_sendButtonActionPerformed
        sendMessage();
    }//GEN-LAST:event_sendButtonActionPerformed

    private void selectTargetsButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectTargetsButtonActionPerformed
    {//GEN-HEADEREND:event_selectTargetsButtonActionPerformed
        MoodleMobileFrame.getInstance().switchPanel(IPanels.SELECT_TARGETS_PANE);
    }//GEN-LAST:event_selectTargetsButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton1;
    private javax.swing.JPanel centerPanel;
    private javax.swing.JButton exitButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea messageTextArea;
    private javax.swing.JButton selectTargetsButton;
    private javax.swing.JButton sendButton;
    private javax.swing.JLabel warningLabel;
    // End of variables declaration//GEN-END:variables

}
